package com.example.black.myapplication.myrevealview;

import android.content.Context;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.widget.HorizontalScrollView;
import android.widget.ImageView;
import android.widget.LinearLayout;

import com.example.black.myapplication.R;

/**
 * https://blog.csdn.net/qq_18983205/article/details/75452162
 */
public class GalleryHorizontalScrollView extends HorizontalScrollView implements View.OnTouchListener {

    public static final String TAG = GalleryHorizontalScrollView.class.getSimpleName();
    private LinearLayout container;
    private int viewWidth;
    private int centerX;
    private ImageView leftImageView;

    public GalleryHorizontalScrollView(Context context) {
        super(context);
        init();
    }

    public GalleryHorizontalScrollView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    public GalleryHorizontalScrollView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
    }


    private void init() {
        //在ScrollView里面放置一个水平线性布局，再往里面放置很多ImageView
        LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
                LinearLayout.LayoutParams.WRAP_CONTENT,
                LinearLayout.LayoutParams.WRAP_CONTENT);
        container = new LinearLayout(getContext());
        container.setLayoutParams(params);
        setOnTouchListener(this);
    }

    @Override
    protected void onLayout(boolean changed, int l, int t, int r, int b) {
        super.onLayout(changed, l, t, r, b);
        View view = container.getChildAt(0);
        viewWidth = view.getWidth();
        centerX = getWidth() / 2;
        centerX = centerX - viewWidth/2;
        container.setPadding(centerX,0,centerX,0);
        //左边距设置一个padding，利用padding将图片一开始顶在中间显示
        //右边距设置一个padding，利用padding留个白，方便最后一张图片可以到中间来
    }

    @Override
    public boolean onTouch(View v, MotionEvent event) {
        if(event.getAction()==MotionEvent.ACTION_MOVE){
            //渐变图片
            reveal();
        }
        return false;
    }

    private void reveal() {
        int scrollX = getScrollX();
        //渐变的图片下标
        int leftIndex = scrollX/viewWidth;
        int rightIndex = leftIndex+1;
        for (int i = 0;i< container.getChildCount();i++ ){
            if (i== leftIndex || i == rightIndex){//利用for循环同时处理左右两张图片的内容
                float ratio = 5000f/viewWidth;//设置成一个固定比例
                leftImageView = (ImageView) container.getChildAt(leftIndex);
                leftImageView.setImageLevel(
                        (int) (5000 - scrollX%viewWidth*ratio)
                );
                Log.d(TAG, "reveal: left:"+(int) (5000 - scrollX%viewWidth*ratio));
                if (rightIndex < container.getChildCount()){
                    ImageView iv_right = (ImageView) container.getChildAt(rightIndex);
                    iv_right.setImageLevel(
                            (int) (10000 -scrollX%viewWidth*ratio)
                    );
                    Log.d(TAG, "reveal: right:"+  (int) (10000 -scrollX%viewWidth*ratio));
                }
            }else {
                //灰色
                ImageView iv = (ImageView) container.getChildAt(i);
                iv.setImageLevel(0);
            }

        }
    }

    //添加图片的方法
    public void addImageViews(Drawable[] revealDrawables){
        for (int i = 0; i < revealDrawables.length; i++) {
            ImageView img = new ImageView(getContext());
            img.setImageDrawable(revealDrawables[i]);
            container.addView(img);
            if(i==0){
                img.setImageLevel(5000);
            }
        }
        addView(container);
    }
}
